home *** CD-ROM | disk | FTP | other *** search
/ Day Cry / Day Cry CD.bin / oh_towns / taropyon / splib / splib.lzh / PRG / LHX / APPEND.C next >
C/C++ Source or Header  |  1992-12-21  |  6KB  |  282 lines

  1. /***********************************************************
  2.         append.c -- append to archive
  3. ***********************************************************/
  4. #include    "lh386.h"
  5.  
  6. #include    <stdio.h>
  7. #include    <stdlib.h>
  8. #include    <string.h>
  9. #include    <io.h>
  10. #include    <dos.h>
  11. #include    "lh.h"
  12. #include    "intrface.h"
  13.  
  14. #ifdef    __HIGHC__
  15. #    pragma    On(Align_labels);
  16. #endif
  17.  
  18. #ifdef    __HIGHC__
  19. extern    long        filesize(FILE * fp);
  20. #endif
  21.  
  22. #define MAX_INDICATOR_COUNT     64
  23. long        indicator_count;
  24. long        indicator_threshold;
  25.  
  26. extern int    compress_method;
  27. extern long int reading_size;
  28. extern unsigned short dicbit;
  29.  
  30. struct interfacing interface;
  31.  
  32. void        start_indicator(char *name, long size, char *msg, long def_indicator_threshold)
  33. {
  34.     long        i;
  35.     int         m;
  36.  
  37.     m = MAX_INDICATOR_COUNT - strlen(name);
  38.     if (m < 0)
  39.         m = 3;                    /* (^_^) */
  40. #ifdef    __MSDOS__
  41.     LHX_fprintf(stderr, "\r%-12s - %s :  ", name, msg);
  42. #else
  43.     LHX_fprintf(stderr, "\r%s\t- %s :  ", name, msg);
  44. #endif
  45.     indicator_threshold =
  46.         ((size + (m * def_indicator_threshold - 1)) /
  47.          (m * def_indicator_threshold) *
  48.          def_indicator_threshold);
  49.     if (indicator_threshold)
  50.         i = ((size + (indicator_threshold - 1)) / indicator_threshold);
  51.     else
  52.         i = 0;
  53.     while (i--)
  54.         LHX_fputc('.', stderr);
  55.     indicator_count = 0;
  56. #ifdef    __MSDOS__
  57.     LHX_fprintf(stderr, "\r%-12s - %s :  ", name, msg);
  58. #else
  59.     LHX_fprintf(stderr, "\r%s\t- %s :  ", name, msg);
  60. #endif
  61.  
  62.     fflush(stderr);
  63.     reading_size = 0L;
  64. }
  65.  
  66. void        finish_indicator2(char *name, char *msg, int pcnt)
  67. {
  68.     if (pcnt > 100)
  69.         pcnt = 100;             /* (^_^) */
  70. #ifdef    __MSDOS__
  71.     LHX_fprintf(stderr, "\r%-12s - %s(%d%%)\n", name, msg, pcnt);
  72. #else
  73.     LHX_fprintf(stderr, "\r%s\t- %s(%d%%)\n", name, msg, pcnt);
  74. #endif
  75.     indicator_threshold = 0;
  76.     fflush(stderr);
  77. }
  78.  
  79. void    finish_indicator(char *name, char *msg)
  80. {
  81. #ifdef    __MSDOS__
  82.     LHX_fprintf(stderr, "\r%-12s - %s\n", name, msg);
  83. #else
  84.     LHX_fprintf(stderr, "\r%s\t- %s\n", name, msg);
  85. #endif
  86.     indicator_threshold = 0;
  87.     fflush(stderr);
  88. }
  89.  
  90. void        copylzh(void)
  91. {
  92.     update_arcstamp();
  93.     writehdr();
  94.     copyfile(file1, file2, hpb.packed, 0);
  95. }
  96.  
  97. static int    isarc(char *name)
  98. {
  99.     static char arc[7][3] =
  100.     {"   ", "zoo", "arc", "pak", "zip", "lzs", "lzh"};
  101.     char       *p;
  102.     int         i;
  103.  
  104.     p = name + strlen(name) - 4;
  105.     if (p < name || *p != '.')
  106.         return 0;
  107.     p++;
  108.     memcpy(arc[0], p, 3);
  109.     for (i = 6; memicmp(arc[i], p, 3); i--);
  110.     return i;
  111. }
  112.  
  113. void        freeze(char *path, char *name)
  114. {
  115.     static int    method = -1;
  116.  
  117.     hpb.pathname = name;
  118.     hpb.filename = getfilename(name);
  119.  
  120.     file3 = myropen(path);
  121.  
  122.     if (method < 0)
  123.     {
  124.         method = 5;
  125.         if (flg_o)
  126.             method = 1;
  127.         if (flg_z == 1)
  128.             method = 0;
  129.         if (method > 0)
  130.             method = encode_alloc(method);
  131.     }
  132.     hpb.attr = 0x20;
  133.     if (flg_a)
  134.     {
  135. #ifdef    __HIGHC__
  136.         unsigned int attr;
  137.         _dos_getfileattr(path, &attr);
  138.         hpb.attr = attr;
  139. #else
  140.         _dos_getfileattr(path, (unsigned int *) &hpb.attr);
  141. #endif
  142.     }
  143.     hpb.utc = getfiletime(file3);
  144.     hpb.level = flg_h;
  145.     hpb.info = 0;
  146. #ifndef __HIGHC__
  147.     hpb.original = hpb.packed = filelength(fileno(file3));
  148. #else
  149.     hpb.original = hpb.packed = filesize(file3);
  150. #endif
  151.     strcpy(hpb.method, "-lh5-");
  152.     interface.method = method;
  153.     if (hpb.original == 0 || flg_z == 2 && isarc(hpb.filename))
  154.         interface.method = 0;
  155.  
  156.     if (interface.method > 0)
  157.     {
  158.         makehdr();
  159.         writehdr();
  160.         interface.infile = file3;
  161.         interface.outfile = file2;
  162.         interface.original = hpb.original;
  163.         start_indicator(hpb.filename, hpb.original, "Freezing", 1 << dicbit);
  164.         encode(&interface);
  165.         if (interface.packed >= interface.original)
  166.         {
  167.             rewind(file3);
  168.             fseek(file2, hpb.currentpos, SEEK_SET);
  169.             interface.method = 0;
  170.         }
  171.         hpb.packed = interface.packed;
  172.     }
  173.     hpb.method[3] = interface.method + '0';
  174.     if (interface.method == 0)
  175.     {
  176.         makehdr();
  177.         writehdr();
  178.         copyfile(file3, file2, hpb.original, 1);
  179.     }
  180.     update_arcstamp();
  181.     hpb.filecrc = crc;
  182.     adjusthdr();
  183.     fclose(file3);
  184.     finish_indicator2(hpb.filename, "Frozen", ratio(hpb.packed, hpb.original, 2));
  185. }
  186.  
  187. static void freeze_fb(struct fb * fp)
  188. {
  189.     char       *p;
  190.     char       *path;
  191.  
  192.     path = fp->fname;
  193.     p = e_malloc(strlen(path) + 1);
  194.     strcpy(p, path);
  195.     freeze(p, p + (fp->cpos - path));
  196.     free(p);
  197. }
  198.  
  199. int         append(void)
  200. {
  201.     struct fb  *fp;
  202.  
  203.     if ((fp = searchfile(hpb.pathname)) != NULL
  204.         && (flg_c || fp->time > hpb.utc))
  205.     {
  206.         freeze_fb(fp);
  207.         return 1;
  208.     } else
  209.     {
  210.         copylzh();
  211.         return 0;
  212.     }
  213. }
  214.  
  215. int         endappend(void)
  216. {
  217.     struct fb  *fp;
  218.     int         modified;
  219.  
  220.     modified = 0;
  221.     for (fp = fbuf; fp != NULL; fp = fp->nxt)
  222.     {
  223.         if (fp->used == 0)
  224.         {
  225.             freeze_fb(fp);
  226.             modified++;
  227.         }
  228.     }
  229.     return modified;
  230. }
  231.  
  232. void        deletefiles(void)
  233. {
  234.     struct fb  *fp;
  235.  
  236.     for (fp = fbuf; fp != NULL; fp = fp->nxt)
  237.     {
  238.         strcpy(work, fp->fname);
  239.         remove(work);
  240.     }
  241. }
  242.  
  243. int         freshen(char *bdir)
  244. {
  245.     char       *path, *name;
  246.     struct find_t srchbuf;
  247.     int         flag;
  248.  
  249.     path = e_malloc(strlen(bdir) + strlen(hpb.pathname) + 1);
  250.     strcpy(path, bdir);
  251.     if (*hpb.pathname == DELIM)
  252.     {
  253.         if (path[1] == ':')
  254.         {
  255.             path[2] = '\0';
  256.         } else
  257.         {
  258.             *path = '\0';
  259.         }
  260.     }
  261.     name = path + strlen(path);
  262.     strcpy(name, hpb.pathname);
  263.     flag = !_dos_findfirst(path, flg_a ? 0x07 : 0, &srchbuf);
  264.     if (flag)
  265.     {
  266.         if (!flg_c)
  267.         {
  268.             if (hpb.utc >= dos2unix((struct ftime *) & (srchbuf.wr_time)))
  269.                 flag = 0;
  270.         }
  271.     }
  272.     if (flag)
  273.     {
  274.         freeze(path, name);
  275.     } else
  276.     {
  277.         copylzh();
  278.     }
  279.     free(path);
  280.     return flag;
  281. }
  282.